from typing import Optional


from src.database.models import User
from src.user_service.schemas import My_account

from src.user_service.service.auth import raise_401_error

from src.database.my_crud import My_crud

user_crud = My_crud(User)


class Search_user_service:

    async def search_account(
        self,
        first_name: Optional[str],
        last_name: Optional[str],
        email: Optional[str],
        from_var: int,
        size: int,
        user_id: Optional[int]
    ):
        await raise_401_error(user_id)

        first_name = ("" if first_name is None else first_name)
        last_name = ("" if last_name is None else last_name)
        email = ("" if email is None else email)

        return await user_crud.get(
            filters=[
                User.firstName.ilike(f"%{first_name}%"),
                User.lastName.ilike(f"%{last_name}%"),
                User.email.ilike(f"%{email}%")
            ],
            Output_model=My_account,
            offset=from_var,
            limit=size,
            multi=True
        )
